package com.liuyk.main;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.annotation.Nullable;

import com.liuyk.main.activity.AidlApkInfoManager;
import com.liuyk.main.activity.ApkInfo;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class ApkInfoManager extends Service {
    //进程安全列表
    private final CopyOnWriteArrayList<ApkInfo> mApkInfos = new CopyOnWriteArrayList<>();

    //当前线程是否销毁
    private final AtomicBoolean mIsServiceDestroy = new AtomicBoolean(false);

    @Override
    public void onCreate() {
        super.onCreate();
        ApkInfo mAidlModel = new ApkInfo();
        mAidlModel.setName("apk");
        mApkInfos.add(mAidlModel);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mIsServiceDestroy.set(true);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new BinderManager();
    }

    class BinderManager extends AidlApkInfoManager.Stub {

        @Override
        public void addApk(ApkInfo apk) throws RemoteException {
            mApkInfos.add(apk);
        }

        @Override
        public ApkInfo getApkInfo() throws RemoteException {
            return mApkInfos.get(0);
        }
    }
}
